/**数据稽查
 * @author wangze
 * @date 2018-01-08 9:17.
 **/
(function () {
    var _bt = window.BT,
        _util = _bt.util,
        _debug = _util.debug,
        _dialog = _util.dialog,
        _config = _util.config,
        _enum = _config.enumValues,
        _url = _config.url,
        _uUrl = _config.url.user,
        _dataAuditUrl = _url.dataAudit;

    var gridOpt = {
        url: _dataAuditUrl.list,
        post: function () {
            var obj = $('#form-search').serializeObject();
            var beginDateKey = 'dateStartStr', endDateKey = 'dateEndStr';
            if (!obj) return obj;
            if (obj[beginDateKey]) {
                obj[beginDateKey] = $.trim(obj[beginDateKey]) + ' 00:00:00';
            }
            if (obj[endDateKey]) {
                obj[endDateKey] = $.trim(obj[endDateKey]) + ' 23:59:59';
            }
            return obj;
        },
        formatters: {
            'timeFormat': function (column, row) {
                if (!row.startdate) {
                    return '';
                }
                var date = new Date(row.startdate);
                return date.Format('yyyy-MM-dd');
            },
            'commands': function (column, row) {
                return '<button class="btn btn-xs btn-purple command-edit" data-row-id="' + row.id + '">管理</button>';
            }
        }
    };
    var page = {
        grid: null,
        init: function () {
            page.grid = new _bt.BGrid(gridOpt);
            page.add.init();
            page.edit.init();
            page.bind();
        },
        resetForm: function (form, v) {
            form[0].reset();
            $('[type=hidden]', form).val('');
            v.resetForm();
            $('.form-group', form).each(function () {
                var $me = $(this);
                $me.removeClass('has-error');
                $('.help-block', $me).remove();
            });
        }
    };


    page.add = {
        id: '#my-add-modal',
        init: function () {
            /**
             * 初始化select
             */
            _util.selectRemoteInit({
                ele: $('[name=platformid]', page.add.id),
                url: _uUrl.platform
            });

            var $form = $('#form-add');

            var $auditType = $('[name=audittype]', $form);

            var v = page.add.addValidate($form);

            //添加
            $('#my-modal-btn-add', page.add.id).on('click', function () {
                $auditType.val(_enum.auditType.add.value);
                $form.submit();
            });
            //移除
            $('#my-modal-btn-del', page.add.id).on('click', function () {
                $auditType.val(_enum.auditType.del.value);
                $form.submit();

            });
            //检索
            $('.btn-solr-search', page.add.id).on('click', function () {

            });

            $(page.add.id).on('show.bs.modal', function () {

            }).on('hidden.bs.modal', function () {
                page.resetForm($form, v);
            });
        },
        show: function () {
            $(page.add.id).modal('show');
        },
        addValidate: function (form) {
            var validateOpt = {
                rules: {
                    platformid: {required: true},
                    searchstrategy: {required: true}
                },
                messages: {
                    platformid: {required: '必填'},
                    searchstrategy: {required: '必填'}
                },
                submitHandler: function (form) {
                    var successEvent = function (data) {
                        if (data && data.flag) {
                            _dialog.info('保存成功');
                            $(page.add.id).modal('hide');
                            page.grid.reload();
                        } else {
                            _dialog.warn('保存失败');
                        }
                    };
                    _util.jsonAjax({
                        url: _dataAuditUrl.save,
                        type: 'post',
                        data: $(form).serializeObject()
                    }, successEvent);
                }
            };

            return form.validate($.extend({}, _util.validate.default, validateOpt));
        }

    };


    /**
     *编辑页面初始化
     */
    page.edit = {
        id: '#my-edit-modal',
        form: '#form-edit',
        addValidate: function (form) {
            var validateOpt = {
                rules: {
                    searchstrategy: {required: true},
                    audittype: {required: true}
                },
                messages: {
                    searchstrategy: {required: '必填'},
                    audittype: {required: '必填'}
                },
                submitHandler: function (form) {
                    var successEvent = function (data) {
                        if (data && data.flag) {
                            _dialog.info('保存成功');
                            $(page.edit.id).modal('hide');
                            page.grid.reload();
                        } else {
                            _dialog.warn('保存失败');
                        }
                    };
                    _util.jsonAjax({
                        url: _dataAuditUrl.save,
                        type: 'post',
                        data: $(form).serializeObject()
                    }, successEvent);
                }
            };
            return form.validate($.extend({}, _util.validate.default, validateOpt));
        },
        init: function () {
            var $form = $(page.edit.form);
            _util.selectEnumInit($('[name=audittype]', page.edit.id), _enum.auditType, '全部');
            page.edit.addValidate($form);
            $('#my-edit-modal-btn-save', page.edit.id).on('click', function () {
                $form.submit();
            });
        },
        show: function (id) {
            var successEvent = function (data) {
                if (data && data.flag) {
                    var $form = $(page.edit.form);
                    data = data.data;
                    $form.bindForm(data);
                    $('#edit-platform-name', $form).text(data.platform);
                    $(page.edit.id).modal('show');
                } else {
                    _dialog.warn('获取数据失败，请重新尝试');
                }
            };
            _util.ajax({
                url: _dataAuditUrl.data,
                type: 'get',
                data: {id: id}
            }, successEvent);


        }
    };

    page.bind = function () {
        var formSearch = '#form-search';
        var searchEvent = function () {
            page.grid.reload();
        };
        var addEvent = function () {
            page.add.show();
        };
        var delEvent = function () {
            //选择数据
            var ids = page.grid.getSelections();
            var confirmEvent = function () {
                _util.ajax({
                    type: 'get',
                    url: _dataAuditUrl.del,
                    data: {ids: ids.join(',')}
                }, function (data) {
                    if (data && data.flag) {
                        _dialog.info('删除成功');
                        page.grid.reload();
                    } else {
                        _dialog.warn('删除失败');
                    }
                });
            };
            if (ids.length === 0) {
                _dialog.warn('请选择一条数据');
                return;
            }
            _dialog.confirm('确定删除吗？', confirmEvent);

        };

        _util.selectRemoteInit({
            ele: $('[name="platformid"]', formSearch),
            url: _uUrl.platform
        });

        _util.selectEnumInit($('[name=audittype]', formSearch), _enum.auditType, '全部');

        $('.input-daterange').datepicker({
            format: 'yyyy-mm-dd',
            language: 'zh',
            autoclose: true
        });

        $('#btn-search').on('click', searchEvent);
        $('#btn-add').on('click', addEvent);
        $('#btn-del').on('click', delEvent);

        /**
         * 绑定grid上的编辑事件
         */
        page.grid.on('loaded', function () {
            $('.command-edit').on('click', function () {
                var id = $(this).attr('data-row-id');
                page.edit.show(id);
                return false;
            });

        });


    };

    $(function () {
        page.init();
    });

})();